package com.example.demo.itext;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
import org.apache.pdfbox.cos.COSDictionary;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.pdmodel.font.PDType0Font;
import org.apache.pdfbox.pdmodel.font.PDType1Font;

import java.io.*;

public class Pdf2Test {
    public static void main(String[] args) throws Exception {
//        generateBlankPdf("测试",null);
        test2();
    }

    public static void generateBlankPdf(String content, OutputStream os) {
        try {
            // 创建一个PDDocument对象
            PDDocument document = new PDDocument();
            // 创建一个页面
            PDPage page = new PDPage(PDRectangle.A4);
            document.addPage(page);
            // 创建页面内容流
            PDPageContentStream contentStream = new PDPageContentStream(document, page);
            // 设置字体和字号

//            contentStream.setFont(msyh, 12);
            File fontFile = new File("C:\\Windows\\Fonts\\msyh.ttc");

            PDType0Font font = PDType0Font.load(document, fontFile);
            contentStream.setFont(font, 20);
            // 在页面上绘制文本
            contentStream.beginText();
            contentStream.newLineAtOffset(100, 700);
            contentStream.showText(content);
            contentStream.endText();
            // 关闭内容流和文档
            contentStream.close();
            document.save("F:\\360MoveData\\Users\\lqq\\Desktop\\image\\合并发票2.pdf");
//            document.save("os");
            document.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void test2() throws Exception {
        //创建文档对象
        Document document = new Document();
        //设置输出流
        PdfWriter.getInstance(document, new FileOutputStream("F:\\360MoveData\\Users\\lqq\\Desktop\\image\\合并发票2.pdf"));
        //打开文档
        document.open();
        //向文档中添加内容
        document.add(new Paragraph("Hello World!"));
        //关闭文档
        document.close();
    }
}
